package protocol.factorial;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.compression.ZlibCodecFactory;
import io.netty.handler.codec.compression.ZlibWrapper;
import io.netty.handler.ssl.SslContext;

public class FactorialServerInitializer extends ChannelInitializer<SocketChannel> {

    private final SslContext sscCtx;

    public FactorialServerInitializer(SslContext sscCtx) {
        this.sscCtx = sscCtx;
    }

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        if (sscCtx != null) {
            pipeline.addLast(sscCtx.newHandler(ch.alloc()));
        }

        // Enable stream compression
        pipeline.addLast(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
        pipeline.addLast(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));

        //Add teh number codec first
        pipeline.addLast(new BigIntegerDecoder());
        pipeline.addLast(new NumberEncoder());


        //business logic
        pipeline.addLast(new FactorialServerHandler());

    }
}
